package com.smalljj.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.Overlay;
import com.smalljj.comm.ActivityStackControlUtil;
import com.smalljj.comm.Constants;

public class NowMeActivity extends MapActivity {

	private PositionReceiver positionReceiver;

	private BMapManager mapManager = null;

	private MapView mapView = null;

	private final static String KEY = "DB9F26EEF56BA800DC9B35F52C2E34AC5D4F9A1F";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除标题
		setContentView(R.layout.nowme);
		ActivityStackControlUtil.add(this);
		
		Log.i(Constants.LOG_TAG, "注册监听...");
		positionReceiver = new PositionReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction("POSITION");
		registerReceiver(positionReceiver, filter);
		Log.i(Constants.LOG_TAG, "注册监听完毕...");
		
		mapManager = new BMapManager(getApplication());
		mapManager.init(KEY, null);
		super.initMapActivity(mapManager);
		
		mapView = (MapView) findViewById(R.id.bmapsView);
		mapView.setBuiltInZoomControls(true); //设置启用内置的缩放控件
		
		//得到mMapView的控制权,可以用它控制和驱动平移和缩放
		MapController mapController = mapView.getController(); 
		// GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
		// (int) (116.404 * 1E6)); //用给定的经纬度构造一个GeoPoint，单位是微度(度* 1E6)
		// mapController.setCenter(point); //设置地图中心点
		mapController.setZoom(12); // 设置地图zoom级别
		mapView.getOverlays().add(new MyOverlay());
	}
	
	public class MyOverlay extends Overlay {
		GeoPoint geoPoint = new GeoPoint((int) (39.915 * 1E6),(int) (116.404 * 1E6));
		Paint paint = new Paint();
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			// 在天安门的位置绘制一个String
			Point point = mapView.getProjection().toPixels(geoPoint, null);
			canvas.drawText("★这里是天安门", point.x, point.y, paint);
		}
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		unregisterReceiver(positionReceiver);
		return false;
	}
	
	public class PositionReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals("POSITION")) {
				if(intent.getStringExtra("changeflag").equalsIgnoreCase("change")){//有改变坐标
					Log.i(Constants.LOG_TAG,"接收到SP_BF_CREATEDATE:" + intent.getStringExtra("SP_BF_CREATEDATE"));
					Log.i(Constants.LOG_TAG,"接收到SP_BF_HOUR:" + intent.getStringExtra("SP_BF_HOUR"));
					Log.i(Constants.LOG_TAG,"接收到SP_BF_MINUTE:" + intent.getStringExtra("SP_BF_MINUTE"));
					Log.i(Constants.LOG_TAG,"接收到SP_BF_LONGITUDE:" + intent.getStringExtra("SP_BF_LONGITUDE"));
					Log.i(Constants.LOG_TAG,"接收到SP_BF_LATITUDE:" + intent.getStringExtra("SP_BF_LATITUDE"));
					Log.i(Constants.LOG_TAG,"接收到SP_BF_STAYTIME:" + intent.getStringExtra("SP_BF_STAYTIME"));
					
					Log.i(Constants.LOG_TAG,"接收到longitude:" + intent.getStringExtra("longitude"));
					Log.i(Constants.LOG_TAG,"接收到latitude:" + intent.getStringExtra("latitude"));
					Log.i(Constants.LOG_TAG,"接收到staytime:" + intent.getStringExtra("staytime"));
				}else if(intent.getStringExtra("changeflag").equalsIgnoreCase("nochange")){
					Log.i(Constants.LOG_TAG,"接收到staytime:" + intent.getStringExtra("staytime"));
				}
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		unregisterReceiver(positionReceiver);
		if (mapManager != null) {
			mapManager.destroy();
			mapManager = null;
		}
		super.onDestroy();
        ActivityStackControlUtil.remove(this); 
	}
	
	@Override
	protected void onPause() {
		unregisterReceiver(positionReceiver);
		if (mapManager != null) {
			mapManager.stop();
		}
		super.onPause();
	}
	
	@Override
	protected void onResume() {
		if (mapManager != null) {
			mapManager.start();
		}
		super.onResume();
	}
}